package com.qxsx.midi.activity.me;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.main.MainActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.AppManager;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Zrj on 2015/9/1.
 */
public class LoginActivity extends Activity implements View.OnClickListener {
    private CustomProgress customProgress;

    private LinearLayout ll_back;//返回
    private EditText et_phone;//手机号
    private EditText et_password;//密码
    private Button btn_login, btn_register;//登录，注册
    private TextView tv_forget;//忘记密码
    private ImageView iv_delect;


    Intent intent = new Intent();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        AppManager.getAppManager().addActivity(this);
        findById();
    }

    private void findById() {
        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);

        et_phone = (EditText) findViewById(R.id.et_phone);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password.setImeOptions(EditorInfo.IME_ACTION_DONE);
        et_password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    //do something;
//                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.hideSoftInputFromWindow(et_password.getWindowToken(), 0);
                    if (checkIsSure()) {
                        login();
                    }
                    return true;
                }
                return false;
            }
        });


        btn_login = (Button) findViewById(R.id.btn_login);
        btn_register = (Button) findViewById(R.id.btn_register);
        tv_forget = (TextView) findViewById(R.id.tv_forget);
        iv_delect = (ImageView) findViewById(R.id.iv_delect);

        iv_delect.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        tv_forget.setOnClickListener(this);

    }


    private void login() {
        customProgress = CustomProgress.show(LoginActivity.this, "登录中....", true, null);
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String client_id = (String) SPUtils.get(AppContext.getContext(), "client_id","");
        try {
            object.put("login_name", et_phone.getText().toString());
            object.put("pwd", et_password.getText().toString());
            object.put("cid", client_id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("101", array,                                                                                                                                          new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        if (code.toString().equals("401")) {
                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                            customProgress.dismiss();
                        } else if (array == null) {
                            Log.w("array", array + "");
                            return;
                        } else {
                            try {
                                JSONObject object1 = array.getJSONObject(0);
                                String user_id = object1.getString("id");
                                //'Log.w("user_id", user_id);
                                SPUtils.put(AppContext.getContext(), "user_id", user_id);
                                SPUtils.put(AppContext.getContext(), "login_name",object1.getString("midi_id"));
                                SPUtils.put(AppContext.getContext(), "login_password", et_password.getText().toString());
                                Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                customProgress.dismiss();
                                finish();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
        );
    }


    /**
     * 账号密码不能为空
     *
     * @param
     */

    private boolean checkIsSure() {
        if (TextUtils.isEmpty(et_phone.getText().toString()) || !Tools.isMobileNO(et_phone.getText().toString().trim())) {
            Toast.makeText(AppContext.getContext(), "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(et_password.getText().toString())) {
            Toast.makeText(AppContext.getContext(), "请输入正确的密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.btn_login:
                if (checkIsSure()) {
                    login();
                }
                break;
            case R.id.btn_register:
                intent.setClass(AppContext.getContext(), RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_forget:
                intent.setClass(AppContext.getContext(), ForgetActivity.class);
                startActivity(intent);
                break;

            case R.id.iv_delect:
                et_phone.setText("");
                break;


            default:
                break;
        }
    }
}
